package tree.nodeType.numeric;

import java.util.ArrayList;
import java.util.Random;

import pools.PoolsManager;
import tree.Node;
import tree.TreeNodesManager;
import tree.nodeType.Numeric;

/**
 * constant node
 *
 */
public class Constant extends Numeric{
	
	private int number;
	
	public Constant(int number,Node parent,TreeNodesManager treeNodesManager,int depth) {
		super(0, parent,depth);
		if (treeNodesManager!=null){
			treeNodesManager.addConstantNode(this);
		}
		this.number=number;
	}
	
	public Constant() {}
	
	public double execute(){
		return number;
	}

	public  ArrayList<Node> createSons(){
		return null;
	}

	@Override
	public String toString() {		
		return "Constant: " +number;
	}

	@Override
	public void initSons(int depth, int maximumDepth,TreeNodesManager treeNodesManager) {		
		
	}
	
	@Override
	public Node getRandNodeWithTheSameReturnType(int maxDepth,TreeNodesManager tnm){
		if (random.nextDouble()>0.5){
			return PoolsManager.get().getRandomConstantNode(getParent(), tnm, getDepth());
		}else{
			getParent().setHasConstSon(false);
			return PoolsManager.get().getRandomNumericFuncNode(getDepth(), maxDepth, getParent(), tnm);
		}
	}

	@Override
	protected ArrayList<Node> getProperNodesManagerList(
			TreeNodesManager treeNodeManager) {	
		return treeNodeManager.getAllConstantsInTree();
	}

	@Override
	public Node getRandNode(int maxDepth, TreeNodesManager treeNodesManager) {		
		return PoolsManager.get().getRandomConstantNode(getParent(), treeNodesManager, getDepth());
	}
	
	//--------Functions needed for XML file--------//
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
   //-----------------------------------------//

	@Override
	protected Node clone(TreeNodesManager treeNodesManager,Node parent) {
		Node cloned= new Constant(this,treeNodesManager,parent);		
		return cloned;
	}
	
	@Override
	protected Node clone(Node parent) {
		Node cloned= new Constant(this,parent);		
		return cloned;
	}
	
	public Constant(Constant constant,Node parent){
		this.depth=constant.depth;
		this.parent=parent;
		this.sonsNeeded=constant.sonsNeeded;
		this.random= new Random();
		this.sons= new ArrayList<Node>();
		this.number=constant.number;

	}
	public Constant(Constant constant, TreeNodesManager treeNodesManager,Node parent){
		super(0,parent,constant.depth);
		if (treeNodesManager!=null){
			treeNodesManager.addConstantNode(this);
		}
		this.number=constant.number;
	}


	

}
